package com.treasury.glasses.controller;

import com.treasury.glasses.Main;
import com.treasury.glasses.controller.helper.Maskable;
import com.treasury.glasses.domain.ActivityLog;
import com.treasury.glasses.service.ActivityService;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * Controller or BackEnd for the FrontEnd Activity mask. Handles the mask
 * functionality and program states.
 * @author <a href="mailto:glasses@audienz.eu">Jean-Luc Burot</a>
 */

public class ActivityController implements Initializable, Maskable  {
    /**
     * Privately handled service class for this controller class.
     */
    private final ActivityService service;
    
    /**
     * Link to the activityList marked control on the mask.
     */
    @FXML
    private TableView activityList;
    
    /**
     * Controller for the Activity mask.
     */
    public ActivityController() {
        this.service = new ActivityService();
    }
    
    private void loadActivityList() {
        // Add column.
        TableColumn dateColumn = new TableColumn("Date");
        dateColumn.setMinWidth(150);
        dateColumn.setCellValueFactory(new PropertyValueFactory<ActivityTableViewData, String>("date"));
        TableColumn activityColumn = new TableColumn("Activity");
        activityColumn.setMinWidth(150);
        activityColumn.setCellValueFactory(new PropertyValueFactory<ActivityTableViewData, String>("activity"));
        this.activityList.getColumns().addAll(dateColumn, activityColumn);
        
        // Load activty of ProgramUser from persistence.
        List<ActivityLog> activities = this.service.getActivities(Main.getLoggedInUser());
        for (ActivityLog activity: activities) {
            this.activityList.getItems().add(
                    new ActivityTableViewData(activity.getCreation().toString(),
                                              activity.getActivityType().toString())
            );
        }
    }
    
    @Override
    public String getName() {
        return "Activity";
    }
    
    @Override
    public final void initialize(final URL url, final ResourceBundle rb) {
        loadActivityList();
    }
}
